<template>
  <div>
    <!-- title；弹窗的标题 -->
    <!-- visible：控制弹窗是否可见 -->
    <!-- before-close：弹窗关闭前的回调函数 -->
    <el-dialog
      :title="info.type === 'add' ? '新增管理员' : '编辑管理员'"
      :visible.sync="info.isShow"
      width="60%"
    >
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="角色名称" prop="roleid">
          <el-select v-model="ruleForm.roleid">
            <el-option
              v-for="(item, index) in optionList"
              :key="index"
              :label="item.rolename"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="管理员名称" prop="username">
          <el-input v-model="ruleForm.username"></el-input>
        </el-form-item>
        <el-form-item label="管理员密码" prop="password">
          <el-input v-model="ruleForm.password"></el-input>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="ruleForm.status">
            <el-radio :label="1">正常</el-radio>
            <el-radio :label="2">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item>
          <el-button
            v-if="info.type === 'add'"
            type="primary"
            @click="submitForm"
            >立即创建</el-button
          >
          <el-button v-else type="primary" @click="submitForm"
            >立即编辑</el-button
          >
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { getRoleList, adminAdd } from "../../api";
export default {
  props: ["info"],
  data() {
    return {
      // 下拉框的数据
      optionList: [],
      // 表单中的数据
      ruleForm: {
        roleid: 3,
        username: "",
        password: "",
        // 1正常2禁用
        status: 1,
      },
      // 校验规则
      rules: {
        roleid: [
          { required: true, message: "请选择角色名称", trigger: "change" },
        ],
        username: [
          { required: true, message: "请输入管理员名称", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    // 获取下拉框中的数据
    this.getOptionList();
  },
  methods: {
    // 设置表单的值
    setVal(data) {
      // 深拷贝，避免数据互相影响
      this.ruleForm = { ...data };
    },
    async getOptionList() {
      let res = await getRoleList();
      this.optionList = res.list;
    },
    submitForm() {
      this.$refs.ruleForm.validate(async (valid) => {
        if (valid) {
          if (this.info.type === "add") {
            // 添加
            let res = await adminAdd(this.ruleForm);
            if (res.code === 200) {
              this.$message({
                type: "success",
                message: res.msg,
              });
            }else {
              this.$message({
                type: "warning",
                message: res.msg,
              });
            }
            // 关闭弹窗
            this.close();
          } else {
            // 编辑
            let res = await editMenu(this.ruleForm);
            // 关闭弹窗
            this.close();
          }
          // 更新list
          this.$emit("update");
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    // 关闭弹窗
    close() {
      this.info.isShow = false;
    },
  },
};
</script>

<style>
</style>